<template>
  <div
    style="
      display: flex;
      flex-direction: column;
      gap: 20px;
      padding: 40px 40px 0 40px;
      align-items: center;
      justify-content: center;
    "
  >
    <img
      src="../../static/image/no-user.png"
      style="width: 300px"
      v-if="error"
    />
    <img src="../../static/image/data-error.png" style="width: 300px" v-else />
    <p v-if="error">{{ error }}</p>
    <p v-else>{{ msg }}</p>
  </div>
</template>

<script>
import { getUser } from '../../api/user'
export default {
  name: 'wx_pages_home_index',
  data() {
    return {
      phone: '',
      redirect: '',
      gisObj: {},
      msg: '加载中...',
      error: '',
    }
  },
  methods: {
    async login() {
      const _this = this
      if (_this.phone) {
        this.$showLoading('加载中...')
        let res = await getUser(_this.phone)
        this.$hideLoading()
        if (res && res.data) {
          _this.$store.dispatch('setUserInfo', {
            ...res.data,
            gisObj: _this.gisObj,
          })
          uni.reLaunch({
            url: _this.redirect ? _this.redirect : `/pages/djzh/index`,
          })
        } else {
          uni.setStorageSync('userInfo', {})
          uni.showModal({
            title: '提示',
            showCancel: false,
            content: res.msg || '未查询到用户信息',
          })
          _this.error = res.msg || '未查询到用户信息'
        }
      }
    },
  },
  onLoad(query) {
    const { phone, gis, redirect, id } = query
    let gisObj = {}
    if (gis) {
      gisObj.lng = gis.split(',')[0]
      gisObj.lat = gis.split(',')[1]
    }
    this.phone = phone
    this.gisObj = gisObj
    this.redirect = redirect
    this.login()
  },
}
</script>
